//
//  XYMainTabBar.swift
//  01-新浪微博
//
//  Created by 陈许亚 on 16/3/6.
//  Copyright © 2016年 cxy. All rights reserved.
//

import UIKit

class XYMainTabBar: UITabBar {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        addSubview(plusBtn)
        
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
    }
    
    //自定义按钮，需要将原来的每一个tabBarButton中的大小减小，将中间位置空出来
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let W = UIScreen.mainScreen().bounds.width / 5
        let H = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: W, height: H)
        var index: CGFloat = 0
        for subview in subviews {
            //判断subview的类型是否是UIButton
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                //改变子控件的frame 类型不匹配，不能直接参与运算
                subview.frame = CGRectOffset(rect, W * index, 0)
                //随着index的变化，frame会变化
//                if index == 1 {
//                    index ++
//                }
//                index ++
                
                index += (index == 1 ? 2 : 1)
            }
            
        }
        addSubview(plusBtn)
        plusBtn.frame = CGRectOffset(rect, W * 2, 0)
//        plusBtn.addTarget(self, action: "plusBtnClick", forControlEvents: .TouchUpInside)
        
    }
//    private func plusBtnClick(){
//    
//        let composeVC = XYComposeViewController()
//        let nav = UINavigationController(rootViewController: composeVC)
//        
//        
//        
//    }
//    

    //MARK 懒加载子视图
    lazy var plusBtn: UIButton = UIFactory.buttonWithImage("tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    
}
